//查询指定条数
function queryAllFilm(currentPage) {
    var pageSize = $("#film_info_tab_pageSize").val() || 10;//每页几条
    currentPage = currentPage || 1; //当前页(不传值就是1)
    $.ajax({
        url: projectName + "/filmController/queryAllFilm",
        data: {"currentPage": currentPage, "pageSize": pageSize},
        async: true,
        type: "post",
        success: function (allData) {
            allData = eval(allData);
            let pageInfo = allData["pageInfo"];
            let totalPage = pageInfo["totalPage"]; //总页数
            fillTable(allData);
            createPageNav(currentPage, totalPage, pageSize);
        }
    });

    //页面大小
    $("#film_info_tab_pageSize").change(function () {
        if (pageSize != $("#film_info_tab_pageSize").val()) {
            pageSize = $("#film_info_tab_pageSize").val()
            queryAllFilm();//查询所有必须的数据
        }
    });
}


//填充表格
function fillTable(allData) {
    let $film_tab_tbody = $("#film_tab_tbody");
    let allFilm = allData["films"]; //获得所有电影
    $film_tab_tbody.children("tr").remove();
    for (let i = 0; i < allFilm.length; i++) {
        $film_tab_tbody.append(
            "            <tr>\n" +
            "                <td>" + allFilm[i].filmId + "</td>\n" +
            "                <td>" + allFilm[i].title + "</td>\n" +
            "                <td>" + allFilm[i].specialFeatures + "</td>\n" +
            "                <td>" + allFilm[i].category + "</td>\n" +
            "                <td>" + allFilm[i].rating + "</td>\n" +
            "                <td>" + allFilm[i].releaseYear + "</td>\n" +
            "                <td class=\"text-center\">\n" +
            "                    <button id=detail" + [[allFilm[i].filmId]] + " class=\"btn btn-info btn-sm btn-detail\">详情</button>\n" +
            "                    <button id=inventory" + [[allFilm[i].filmId]] + " class=\"btn btn-primary btn-sm btn-inventory\">库存</button>\n" +
            "                </td>\n" +
            "            </tr>"
        );

    }
    //详情按钮
    $("#film_tab_tbody .btn-detail").click(function (event) {
        let filmId = this.id.replace(/detail/, "");
        for (let i = 0; i < allFilm.length; i++) {
            if (filmId == allFilm[i].filmId) {
                fillModal(allFilm[i]); //填充并调用模态框
                break;
            }
        }
    });

    //库存按钮(当前库存 = 原本库存 - 出借未还)
    $("#film_tab_tbody .btn-inventory").click(function () {
        //获取电影id
        let filmId = this.id.replace(/inventory/, "");
        $.ajax({
            url: projectName + "/storeController/getCurrentFilmCount",
            data: {"filmId": filmId},
            async: true,//true表示异步
            type: "post",
            success: function (storeInfo) {
                storeInfo = eval(storeInfo);
                $("#modalTitle").text("库存");
                $("#modalBody").empty();
                if (storeInfo == undefined || storeInfo == null) {//没有库存
                    $("#modalBody").append(
                        "                <div class=\"alert alert-danger\">\n" +
                        "                    <h3>没有库存</h3>\n" +
                        "                </div>"
                    );

                } else {//有库存
                    for (let i = 0; i < storeInfo.length; i++) {
                        let totalAddress =
                            storeInfo[i].country + "：" + storeInfo[i].district + "/" + storeInfo[i].city + "/" + storeInfo[i].address;

                        $("#modalBody").append(
                            "                <div class=\"alert alert-warning\">\n" +
                            "                    <h4>地址：" + totalAddress + "</h4>\n" +
                            "                    <h4>库存：" + storeInfo[i].filmCount + "</h4>\n" +
                            "                    <h4>电话：" + storeInfo[i].phone + "</h4>\n" +
                            "                    <h4>邮编：" + storeInfo[i].postalCode + "</h4>\n" +
                            "                </div>"
                        );
                    }
                }
                $("#myModal").modal();
            },
            error: function () {
                alert("程序出错");
            }
        });
    });
}


//生成表格页码导航
function createPageNav(currentPage, totalPage) {
    let prePage =
        "<li id=\"prePage\">\n" +
        "<a href=\"javascript:;\">\n" +
        "<span>&laquo;</span>\n" +
        "</a>\n" +
        "</li>";
    let nextPage =
        "<li id=\"nextPage\">\n" +
        " <a href=\"javascript:;\">\n" +
        " <span>&raquo;</span>\n" +
        "</a>\n" +
        "</li> ";
    let gotoBtn =
        "<li class=\"dropup\">\n" +
        "<a href=\"javascript:;\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">\n" +
        "GOTO\n" +
        "<span class=\"caret\"></span>\n" +
        "</a>\n" +
        "<ul id=\"gotoMenu\" class=\"dropdown-menu\" style=\"max-height: 250px; overflow-y: scroll;\">\n" +
        "</ul>\n" +
        "</li>";

    let pageList = "";
    let pageNums = [];
    let startNum = currentPage - 5;
    let endNum = currentPage + 4;    //-5和+4让当前选择的页码保持在中间
    if (currentPage - 5 < 1) {
        startNum = 1;
        endNum = (totalPage > 10) ? 10 : totalPage;
    }
    if (currentPage + 4 > totalPage) {
        startNum = (totalPage - 9) > 0 ? totalPage - 9 : 1;
        endNum = totalPage;
    }
    for (let i = startNum; i <= endNum; i++) {
        pageNums.push(i);
    }
    for (let j = 0; j < pageNums.length; j++) {
        if (currentPage == pageNums[j]) {
            pageList += ("<li class=\"active page-list\"><a href=\"javascript:;\">" + pageNums[j] + "</a></li>");
        } else {
            pageList += ("<li class=\" page-list\"><a href=\"javascript:;\">" + pageNums[j] + "</a></li>");
        }
    }

    $("#film_info_tab_pagination").html(
        prePage +
        pageList +
        nextPage +
        gotoBtn
    );
    //上一页和下一页按钮的可用性
    currentPage == 1 ? $("#prePage").addClass("disabled") : $("#prePage").removeClass("disabled");
    currentPage == totalPage ? $("#nextPage").addClass("disabled") : $("#nextPage").removeClass("disabled");

    //生成goto里面的数据
    for (let i = 1; i <= totalPage; i++) {
        if (i == currentPage) {
            $("#gotoMenu").append(
                "<li class=\"active goto-list\"><a href=\"javascript:;\">第" + i + "页</a></li>\n"
            );
        } else {
            $("#gotoMenu").append(
                "<li class=\" goto-list\"><a href=\"javascript:;\">第" + i + "页</a></li>\n"
            );
        }
    }
    //goto点击事件
    $("#gotoMenu .goto-list").click(function () {
        let targetPage = parseInt($(this).text().replace(/[^0-9]/gi, ""));
        if (currentPage != targetPage) {
            currentPage = targetPage;
            queryAllFilm(currentPage);//查询所有必须的数据

        }
    });

    //点击页码事件
    $("#film_info_tab_pagination .page-list").click(function () {
        let targetPage = parseInt($(this).text());

        if (targetPage != currentPage) {
            currentPage = targetPage;
            queryAllFilm(currentPage);//查询所有必须的数据

        }
    });

    //上一页事件
    $("#prePage").click(function () {
        if (currentPage != 1) {
            currentPage--;
            queryAllFilm(currentPage);//查询所有必须的数据

        }
    });
    //下一页事件
    $("#nextPage").click(function () {
        if (currentPage != totalPage) {
            currentPage++;
            queryAllFilm(currentPage);//查询所有必须的数据
        }
    });
}

//填充模态框(详情按钮)
function fillModal(modalData) {
    let actorName = modalData["actorName"].join("， ");
    $("#modalBody").empty();
    $("#modalTitle").text(modalData["title"]);
    $("#modalBody").html(
        "<table class=\"table table-striped table-bordered table-hover table-condensed \">\n" +
        "<thead>\n" +
        "<th>语言</th>\n" +
        "<th>原版语言id</th>\n" +
        "<th>租赁期限</th>\n" +
        "<th>租金率</th>\n" +
        "<th>时长</th>\n" +
        "<th>重置成本</th>\n" +
        "</thead>\n" +
        "<tbody>\n" +
        "<tr>" +
        "<td>" + modalData["language"] + "</td>\n" +
        "<td>" + modalData["originalLanguageId"] + "</td>\n" +
        "<td>" + modalData["rentalDuration"] + "</td>\n" +
        "<td>" + modalData["rentalRate"] + "</td>\n" +
        "<td>" + modalData["length"] + "</td>\n" +
        "<td>" + modalData["replacementCost"] + "</td>\n" +
        "</tr>" +
        "</tbody>\n" +
        "</table>\n" +
        "<label for=\"actorName\">演员表</label>\n" +
        "<textarea rows=\"5\" id=\"actorName\"  class=\"form-control\" readonly style=\"resize: none\">\n" +
        actorName +
        "</textarea><br>\n" +
        "<label for=\"description\">简介</label>\n" +
        "<textarea rows=\"5\" id=\"description\"  class=\"form-control\" readonly style=\"resize: none\">\n" +
        modalData["description"] +
        "</textarea>"
    );
    $("#myModal").modal();

}